//
//  TalkFunIntroductionToTestPapers.swift
//  test
//
//  Created by 莫瑞权 on 2021/12/8.
//

import UIKit

class TalkFunIntroductionToTestPapers: TalkfunHitTestView {
    enum closeOrBackState {
        //关闭
        case close
        //返回
        case back
     
    }
    var isAddFillet: Bool = false
    //关闭或返回
    var closeOrBack:TalkfunIncreaseButton = TalkfunIncreaseButton.init()
    //封面
    var testPaperCover:UIImageView = UIImageView.init()
    //标题
    var title:UILabel = UILabel.init()
    //题目数量
    var amount:UILabel = UILabel.init()
    //倒计时
    var countdown:UILabel = UILabel.init()
    //立即作答
    @objc public var answerNow:UIButton = UIButton.init()
    @objc public var YEqualsHigh = false
    var timer:Timer!
    @objc public var examinationAddResult:TFQuestionnairePushResult!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)


        self.closeOrBack.layer.cornerRadius = 2;
        self.addSubview(self.closeOrBack)
        self.closeOrBackStateSwitch(state: closeOrBackState.close)
        
        self.closeOrBack.backgroundColor = UIColor.clear
        self.closeOrBack.addTarget(self, action: #selector(self.closeOrBackClick), for: UIControl.Event.touchUpInside)
        
        self.addSubview(self.testPaperCover)
        self.testPaperCover.image = UIImage.init(named: "HTLive.bundle/TFTestPaperCover")
        
        
        self.testPaperCover.addSubview(self.countdown)
        self.countdown.textAlignment = NSTextAlignment.center
        self.countdown.numberOfLines = 0

       
        
        self.testPaperCover.addSubview(self.amount)
        self.amount.textAlignment = NSTextAlignment.center
        self.amount.numberOfLines = 0
//        self.amountNumber(number: 999)
      
        
        self.testPaperCover.addSubview(self.title)
        self.title.textAlignment = NSTextAlignment.center
        self.titleStr(str: "高中一年级物理能量守恒定律应用测试题恒定律应用测试题恒定律应用测试题")
       
        
        self.addSubview(self.answerNow)
        self.answerNow.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
        self.answerNow.layer.cornerRadius = 4;
        self.answerNow.setTitle("立即作答", for: UIControl.State.normal)
        self.answerNow.titleLabel?.font = UIFont(name: "PingFang SC", size: 17)

    }


    override func layoutSubviews() {
        super.layoutSubviews()
        
        if (self.isAddFillet == false)
        {
            let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.topLeft], cornerRadii: CGSize(width: 10, height: 10))
            
            let  maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            maskLayer.path = maskPath.cgPath
            
            self.layer.mask = maskLayer
            self.isAddFillet = true
        }

        let closeOrBackY = 15.0
        let closeOrBackW = 15.0
        let closeOrBackX = self.frame.width - 15.0 - 15.0
        let closeOrBackH = 15.0
        
        self.closeOrBack.frame = CGRect(x: closeOrBackX, y: closeOrBackY, width: closeOrBackW, height: closeOrBackH)
        
        let testPaperCoverW = 258.0
        let testPaperCoverH = 203.0
        let testPaperCoverX = (self.frame.width - testPaperCoverW)/2.0
        let testPaperCoverY = 52.0
        self.testPaperCover.frame = CGRect(x: testPaperCoverX, y: testPaperCoverY, width: testPaperCoverW, height: testPaperCoverH)
        
        let countdownH = 21.0
        let countdownY = self.testPaperCover.frame.height - countdownH - 25.0
        self.countdown.frame = CGRect(x: 0.0, y: countdownY, width: self.testPaperCover.frame.width, height: countdownH)
        
        let amountH = 21.0
        let amountY = self.countdown.frame.origin.y - 8.0 - amountH
        let amountX = 0.0
        let amountW = self.testPaperCover.frame.width
        self.amount.frame = CGRect(x: amountX, y: amountY, width: amountW, height: amountH)
        
        let titleW = 205.0
        let titleH = 51.5
        let titleX = (self.testPaperCover.frame.width - titleW)/2.0
        let titleY = self.amount.frame.origin.y - 8.0 - titleH
        self.title.frame = CGRect(x: titleX, y: titleY, width: titleW, height: titleH)
        
        let answerNowW = 200.0
        let answerNowH = 40.0
        let answerNowX = (self.frame.width - answerNowW)/2.0
        let answerNowY = self.testPaperCover.frame.maxY + 41.0
        self.answerNow.frame = CGRect(x: answerNowX, y: answerNowY, width: answerNowW, height: answerNowH)
    }
    
    @objc func closeOrBackClick(btn:UIButton){
        
      
        if (btn.isSelected)
        {
            self.closeClick()
        }else{
//            print("触发关闭窗口")
            
        }


        
    }
    
    @objc func answerNowClick(btn:UIButton){
        
          print("作答中")
       
    }
    
    //开始倒计时 duration = 3600
    @objc func openCountdown(time:NSString)
    {

        let countdownStr = NSString(format:"倒计时：%@",  time)
        let attrString = NSMutableAttributedString(string: countdownStr as String)
        let font =  UIFont(name: "PingFang SC", size: 15) as Any

        let attr: [NSAttributedString.Key : Any] = [.font:font,.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13,alpha:1), ]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        //设置默认灰色
        let strSubAttr1: [NSMutableAttributedString.Key: Any] = [.font:font,.foregroundColor: UIColor(red: 0.53, green: 0.53, blue: 0.53,alpha:1)]
        attrString.addAttributes(strSubAttr1, range: NSRange(location: 0, length: 4))
        //设置默认黑色
        let strSubAttr2: [NSMutableAttributedString.Key: Any] = [.font: font,.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13,alpha:1)]
        attrString.addAttributes(strSubAttr2, range: NSRange(location: 4, length: 4))
        self.countdown.attributedText = attrString
    }
    func amountNumber(number:NSInteger)
    {
        let amountStr = NSString(format: "共 %ld 道题", number)
        let amountAttrString = NSMutableAttributedString(string: amountStr as String)
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 15) as Any,.foregroundColor: UIColor(red: 0.53, green: 0.53, blue: 0.53,alpha:1), ]
        amountAttrString.addAttributes(attr, range: NSRange(location: 0, length: amountAttrString.length))
        self.amount.attributedText = amountAttrString
    }
    func titleStr(str:NSString)
    {
        
        let titleAttrString = NSMutableAttributedString(string: str as String)
        self.title.numberOfLines = 0
        let titleAttr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 18) as Any,.foregroundColor: UIColor(red: 0.08, green: 0.08, blue: 0.08,alpha:1), ]
        titleAttrString.addAttributes(titleAttr, range: NSRange(location: 0, length: titleAttrString.length))
        self.title.attributedText = titleAttrString
        

    }
    
    func closeOrBackStateSwitch(state:closeOrBackState)
    {
        if (state == closeOrBackState.close)
        {
            self.closeOrBack.setImage(UIImage.init(named: "HTLive.bundle/examinatioCclose_slices"), for: UIControl.State.normal)
            self.closeOrBack.isSelected = true
        }else if (state == closeOrBackState.back)
        {
            self.closeOrBack.setImage(UIImage.init(named: "HTLive.bundle/TFreturn"), for: UIControl.State.selected)
           
            self.closeOrBack.isSelected = false
        }

    }
    @objc public func addData(model:TFQuestionnairePushResult ,isShow:Bool)
    {

        self.YEqualsHigh = false
        if ((self.superview) != nil) {
            self.superview?.addSubview(self)
        }
        UIView.animate(withDuration: 0.5) {
            self.frame = CGRect(x: self.frame.origin.x, y:0, width: self.frame.size.width, height: self.frame.size.height)
           
        }
        
        self.examinationAddResult = model
        self.title.text = model.title
        self.amountNumber(number: model.subjectTotal)
        self.timerInvalidate()
        self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(setRtxt), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
        self.timer.fire()
        
    }
    @objc func setRtxt() {
        
        if (self.examinationAddResult.endTime >= TalkfunSharedTools.getCurrentDateString()  )
            
        {
            
            let  timeDifference = self.examinationAddResult.endTime - TalkfunSharedTools.getCurrentDateString()
            
            let countdownStr = NSString(format:"%@",  TalkfunSharedTools.getCountdownWithTime(timeDifference))
            
            self.openCountdown(time: countdownStr as NSString)
            if (self.examinationAddResult.endTime == TalkfunSharedTools.getCurrentDateString() )
            {
                self.timerInvalidate()
            }
        }
    }
    @objc func timerInvalidate() {
       
        if((self.timer) != nil)
        {
            self.timer.invalidate()
            self.timer = nil
        }
    }
    @objc func closeClick(){

        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.YEqualsHigh = true
            self.frame = CGRect(x: self.frame.origin.x, y: self.frame.size.height, width: self.frame.size.width, height: self.frame.size.height)
        } completion: { Bool in
            
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
   
}
